package main

import (
	"flag"
	"fmt"
	"iron_man_comm/src/goroslib"
	"iron_man_comm/src/goroslib/dns"
	"iron_man_comm/src/goroslib/pkg/msgs/std_msgs"
	"iron_man_comm/src/tools/log"
	"os"
	"time"
)

func main() {
	log.InitializeLog()

	data := flag.Int("i", 0, "int8 data")
	ip := flag.String("ip", "127.0.0.1", "主机ip")
	flag.Parse()

	log.Info.Printf("data: %d, ip: %s", *data, *ip)

	dns.GetDNSCacheInterface().UpdateDNSMapping("rk3562-buildroot", *ip)
	node, err := goroslib.NewNode(goroslib.NodeConf{
		Name:          fmt.Sprintf("self_test_cmd_%d", time.Now().UnixMilli()),
		MasterAddress: fmt.Sprintf("%s:11311", *ip),
	})
	if err != nil {
		log.Error.Println("create node fail, err: ", err.Error())
		return
	}
	defer node.Close()

	log.Info.Println("create node pass")

	publisher, err := goroslib.NewPublisher(goroslib.PublisherConf{
		Node:  node,
		Topic: "/BoolMainNode/SelfTestCmd",
		Msg:   &std_msgs.Int8{},
		Latch: true,
	})
	if nil != err {
		log.Error.Println("create publisher fail, err: ", err.Error())
		return
	}
	defer publisher.Close()

	log.Info.Println("create publisher pass")

	msg := std_msgs.Int8{
		Data: int8(*data),
	}
	publisher.Write(&msg)

	log.Info.Println("send data pass")

	c := make(chan os.Signal, 1)
	_ = <-c
}
